using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class Cylinder : PPBase
	{
		[Header("Basic parameters")]
		public float radius = 0.5f;

		public float height = 1f;

		[Header("Segments")]
		public int sides = 20;

		public int capSegs = 2;

		public int heightSegs = 2;

		[Header("Slice")]
		public bool sliceOn;

		public float sliceFrom;

		public float sliceTo = 360f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "Cylinder";
			radius = Mathf.Clamp(radius, 1E-05f, 10000f);
			height = Mathf.Clamp(height, 1E-05f, 10000f);
			sides = Mathf.Clamp(sides, 3, 100);
			capSegs = Mathf.Clamp(capSegs, 1, 100);
			heightSegs = Mathf.Clamp(heightSegs, 1, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			float num = height * 0.5f;
			CreateCylinder(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, height, radius, sides, heightSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
			CreateCircle(pivotOffset + m_rotation * new Vector3(0f, num, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			CreateCircle(pivotOffset + m_rotation * new Vector3(0f, 0f - num, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
			if (sliceOn)
			{
				Vector3 vector = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f))) * radius * 0.5f;
				Vector3 vector2 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f))) * radius * 0.5f;
				Vector2 vector3 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(0.5f, 1f));
				vector3 = new Vector2(UVTiling.x * vector3.x, UVTiling.y * vector3.y);
				CreatePlane(pivotOffset + m_rotation * vector, m_rotation * Vector3.up, m_rotation * -vector.normalized, radius, height, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset, vector3, flipNormals);
				CreatePlane(pivotOffset + m_rotation * vector2, m_rotation * Vector3.up, m_rotation * vector2.normalized, radius, height, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, 0f), vector3, flipNormals);
			}
		}
	}
}
